1. 进入ssh文件下

cd ~/.ssh

2. 生成ssh key

ssh-keygen -t rsa -C 你的油箱 

3. 提示输入目录,目录可以不填,有默认路径,直接回车

# ‘()’里面是默认路径
Enter file in which to save the key (/Users/user/.ssh/id_rsa):

4. 提示输入密码, 密码可以不填, 默认没有密码,直接回车

5. 提示再次输入密码, 直接回车,

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

6. 当出现以下图案, 则表示生成 成功

Your identification has been saved in /Users/user/.ssh/id_rsa.
Your public key has been saved in /Users/user/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:IQP0sSMslL9VG+HXO9oqlnFNTkeI1/m9fBiBoKIcdxk  [nanananamln@163.com](mailto:nanananamln@163.com)  
The key’s randomart image is:
+—[RSA 2048]——+
|  .oo . E..o + . |
| … o +o+..+ =    |
|  ..+ O.*o…. o.  |
|   o.=.*.o  o.o o|
|    oo  S  =o..o.|
|    .   . .oo..o.|
|         +. .   .|
|        +  .     |
|       . ..      |
+——[SHA256]——+

7. 打开*.pub复制生成的ssh key 粘贴到Git上

# 路径是上面的默认路径 
sudo vi /Users/malina/.ssh/id_rsa.pub

扩展:生成多个ssh key

1.当要生成多个ssh key时(比如github,gitlab等),则需要在第3步里添加目录以便区分

Enter file in which to save the key (/Users/user/.ssh/id_rsa):github_rsa
Enter file in which to save the key (/Users/user/.ssh/id_rsa):gitlub_rsa

# 因为已经在.ssh文件夹里面,所以前面路径不用写 

2.添加私钥

# 一般私钥会自动添加 

# 查看私钥 
ssh-add -l

# 删除私钥 
ssh-add -D

# 添加私钥 
ssh-add github_rsa
ssh-add gitlub_rsa

# 因为已经在.ssh文件夹里面,所以前面路径不用写,否则要写成ssh-add ~/.ssh/gitlub_rsa 

3.配置config文件

# ssh文件里面没有config直接创建即可
# 创建/更改 
vim config

# 内容为: 
# github
Host  [github.com](http:#github.com/)  
    HostName  [github.com](http:#github.com/)  
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github_rsa
# gitlab
Host  [gitlab.readtv.cn](http:#gitlab.readtv.cn/)  
    HostName  [gitlab.readtv.cn](http:#gitlab.readtv.cn/)  
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/gitlab_rsa

💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕💕

第四步设置了密码
那么每次执行git pull或者git push
将会提示Enter passphrase for key '/Users/user/.ssh/id_rsa':,解决办法如下。

1.命令行执行:

ssh-keygen -p

2.提示如下,不用管直接回车

Enter file in which the key is(/c/Users/YourUserName/.ssh/id_rsa):

3.输入老密码和新密码,输入新密码时直接回车即可


女码农的自我拉扯
24 声望5 粉丝